package com.duanjw.learning.mybatis;

import com.duanjw.learning.mybatis.mapper.UserMapper;
import com.duanjw.learning.mybatis.pojo.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class XmlConfigTest {
    public static void main(String[] args) throws Exception {
        init();
    }

    public static void init() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        SqlSession session = sqlSessionFactory.openSession();
//        Object selectMapById = session.selectOne("com.duanjw.learning.mybatis.mapper.UserMapper.selectMapById", 1);
//        System.out.println(selectMapById);
//
//        User selectById = session.selectOne("com.duanjw.learning.mybatis.mapper.UserMapper.selectById", 1);
//        System.out.println(selectById.toString());
//
//
//        User user = session.getMapper(UserMapper.class).selectById(1);
//        System.out.println(user.toString());

//        User insertUser = new User();
//        insertUser.setId(1);
//        insertUser.setName("张");
//        UserMapper mapper = session.getMapper(UserMapper.class);
//        mapper.insert(insertUser);
//        session.commit();
//
//        System.out.println(session.getMapper(UserMapper.class).selectById(1).toString());
//
        User updateUser = new User();
        updateUser.setId(1);
        updateUser.setName("张-1");
        session.getMapper(UserMapper.class).updateById(updateUser);
//        session.commit();
//        System.out.println(session.getMapper(UserMapper.class).selectById(1).toString());

        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> list = mapper.list();
        System.out.println(list);
//
//
//        session.getMapper(UserMapper.class).deleteById(1);
//        session.commit();
//        System.out.println(session.getMapper(UserMapper.class).list());
    }

    public static void cache() throws Exception{

    }


}
